import { Component, ViewChild} from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import {Tabs} from "./Tabs.enum";

@IonicPage()
@Component({
  selector: 'page-bs-tabs',
  templateUrl: 'bs-tabs.html',
})
export class BsTabsPage {

    type=1;
    tabs = Tabs;
    selTab:string;
    @ViewChild("tabNav") tabNav:NavController;
    constructor(public navCtrl: NavController, public navParams: NavParams) {
    }

    ionViewDidLoad() {
        let tab = this.navParams.get('tab');
        if(tab){
            this.changeTab(tab,this.navParams.data);
        }else{
            this.changeTab(this.tabs.BsAmericaPage,this.navParams.data);
        }
    }

    changeTab(tab,params?) {
        if(this.tabs[tab]){
            let name = this.tabs[tab];
            if(/^[-+]?\d*$/.test(this.tabs[tab])){
                name = tab;
            }

            let view;
            this.tabNav.getViews().forEach(item=>{
                if(item.id == name){
                    view = item;
                }
            });

            this.tabNav.push(view||name,params,{animate:false}).then(_=>{
                this.selTab = name;
            }).catch(_=>{
                console.error('Tab切换失败：',_);
            });

            /*this.tabNav.setRoot(name,params).then(_=>{
                this.selTab = name;
            }).catch(_=>{
                console.error('Tab切换失败：',_);
            });*/
        }
    }

    //赛事类型改变
    typeChange(type){
        if(type !== this.type){
            this.type = type;
            this.changeTab(this.selTab,{tab:this.selTab,type:this.type});
        }
    }

    //地区改变
    regionChange(tab){
        if(this.tabs[this.selTab] != tab){
            this.changeTab(tab,{type:this.type,tab:tab});
        }
    }

}
